#!/usr/bin/env python3
"""
详细的页面检查脚本
检查具体的JavaScript文件内容
"""

import requests
import re

def check_js_content():
    """检查JavaScript文件内容"""
    base_url = "http://localhost:5173"
    
    print("🔍 检查JavaScript文件内容...")
    
    # 检查main.tsx
    print("\n📄 检查 main.tsx:")
    try:
        response = requests.get(f"{base_url}/src/main.tsx")
        if response.status_code == 200:
            content = response.text
            print(f"✅ 文件大小: {len(content)} 字符")
            
            # 检查是否有错误
            if "error" in content.lower():
                print("⚠️  文件可能包含错误")
            
            # 检查关键内容
            if "createRoot" in content:
                print("✅ 包含 createRoot")
            if "App" in content:
                print("✅ 包含 App 组件")
            if "StrictMode" in content:
                print("✅ 包含 StrictMode")
                
            # 显示前500个字符
            print(f"📝 内容预览:\n{content[:500]}...")
        else:
            print(f"❌ 无法访问 main.tsx: {response.status_code}")
    except Exception as e:
        print(f"❌ 检查 main.tsx 时出错: {e}")
    
    # 检查App.tsx
    print("\n📄 检查 App.tsx:")
    try:
        response = requests.get(f"{base_url}/src/App.tsx")
        if response.status_code == 200:
            content = response.text
            print(f"✅ 文件大小: {len(content)} 字符")
            
            # 检查关键内容
            if "StartupScreen" in content:
                print("✅ 包含 StartupScreen")
            if "LoginScreen" in content:
                print("✅ 包含 LoginScreen")
            if "Layout" in content:
                print("✅ 包含 Layout")
                
            # 显示前500个字符
            print(f"📝 内容预览:\n{content[:500]}...")
        else:
            print(f"❌ 无法访问 App.tsx: {response.status_code}")
    except Exception as e:
        print(f"❌ 检查 App.tsx 时出错: {e}")
    
    # 检查useHmiState.ts
    print("\n📄 检查 useHmiState.ts:")
    try:
        response = requests.get(f"{base_url}/src/hooks/useHmiState.ts")
        if response.status_code == 200:
            content = response.text
            print(f"✅ 文件大小: {len(content)} 字符")
            
            # 检查关键内容
            if "DetectedObject" in content:
                print("✅ 包含 DetectedObject 类型")
            if "useState" in content:
                print("✅ 包含 useState")
            if "useEffect" in content:
                print("✅ 包含 useEffect")
                
            # 检查是否有导入错误
            if "import.*from.*types" in content:
                print("⚠️  可能包含类型导入")
            if "import.*from.*utils" in content:
                print("⚠️  可能包含工具导入")
                
        else:
            print(f"❌ 无法访问 useHmiState.ts: {response.status_code}")
    except Exception as e:
        print(f"❌ 检查 useHmiState.ts 时出错: {e}")

def check_html_structure():
    """检查HTML结构"""
    print("\n🔍 检查HTML结构...")
    
    try:
        response = requests.get("http://localhost:5173")
        if response.status_code == 200:
            content = response.text
            
            # 检查根元素
            root_match = re.search(r'<div id="root">(.*?)</div>', content, re.DOTALL)
            if root_match:
                root_content = root_match.group(1).strip()
                print(f"🎯 根元素内容: '{root_content}'")
                if not root_content:
                    print("❌ 根元素为空 - React应用未渲染")
                else:
                    print("✅ 根元素有内容")
            else:
                print("❌ 未找到根元素")
            
            # 检查脚本标签
            script_matches = re.findall(r'<script[^>]*src="([^"]*)"[^>]*>', content)
            print(f"📜 发现 {len(script_matches)} 个脚本文件")
            for script in script_matches:
                print(f"  - {script}")
            
            # 检查是否有错误信息
            if "error" in content.lower():
                print("⚠️  HTML中可能包含错误信息")
            
        else:
            print(f"❌ 无法访问页面: {response.status_code}")
    except Exception as e:
        print(f"❌ 检查HTML结构时出错: {e}")

def main():
    """主函数"""
    print("🚀 开始详细检查HMI原型页面...")
    
    check_html_structure()
    check_js_content()
    
    print("\n✅ 详细检查完成")

if __name__ == "__main__":
    main()
